package com.example.demo.security;

import com.alibaba.fastjson.JSON;
import com.example.demo.Result;
import com.example.demo.Utils.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.val;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import java.io.IOException;

public class LogoutSuccessHandlerService implements LogoutSuccessHandler {
    @Resource
    private JwtUtil jwtUtil;

    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        String token = request.getHeader("Authorization");
        if (jwtUtil.validateToken(token)) {
            jwtUtil.validateToken(token);
        }
        Result<String> success = Result.success("退出成功!");
        String jsonString = JSON.toJSONString(success);
        response.getWriter().write(jsonString);
    }
}
